package com.example.sprintbootarithmetic.DataStructure.stack.PolandNotation;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PolandNotation {
    public static void main(String[] args) {
        //����һ���沨�����ʽ
        String suffixExpression = "4 5 * 8 - 6 + 8";
        List<String> list = getInstance(suffixExpression);
        list.forEach(System.out::println);
        int res = caseall(list);
    }

    public static List<String> getInstance(String suffixExpression) {
        List<String> list = new ArrayList<>();
        String[] suffix = suffixExpression.split("");
        for (String item : suffix) {
            if (!item.equals(" ")) {
                list.add(item);
            }
        }
        return list;
    }

    public static int caseall(List<String> list) {
        Stack<String> stack = new Stack<>();
        for (String item : list) {
            if (item.matches("\\d+")) {
                stack.push(item);
            } else {
                int num1 = Integer.parseInt(stack.pop());
                int num2 = Integer.parseInt(stack.pop());
                int res = 0;
                if (item.equals("+")) {
                    res = num1 + num2;
                } else if (item.equals("-")) {
                    res = num2 - num1;
                } else if (item.equals("*")) {
                    res = num1 * num2;
                } else if (item.equals("/")) {
                    res = num2 / num1;
                } else {
                    new Exception("�����쳣");
                }
                stack.push(res + "");
            }
        }
        return Integer.parseInt(stack.pop());
    }
}
